<template>
<div class="jjmap" v-loading='isLoading || isloading' element-loading-background="rgba(0, 0, 0, 0.3)" element-loading-text="正在加载">
  <div class="header">{{chain_name}}产业链集聚图</div>
  <div class="city_list">
    <div v-for="(item,index) in cityList" :key="index" @click="changeCity(index,item)">{{item}}</div>
  </div>
  <div id="jjmap"></div>
  <div class="com_list">
    <div class="header_more" @click="showList"><span class="title">{{chain_name}}</span> 更多( {{ total>=10000?10000+'+':total }} )</div>
    <div class="list">
       <div class="item-c" v-for="(item) in companyList" :key='item.id' @click="toDetail(item)">
        <div class="flex1">
          <div class="c-left">
            <div class="score">{{item.score}}</div>
            <div class="cH">得分</div>
          </div>
          <div class="c-right">
            <div class="c-title c-b">{{item.company_name}}</div>
            <div class="c-title cH">电话：{{item.elephone}}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</template>
<script>
import "../../../../node_modules/echarts/map/js/china";
import {  chain_city_map , getMap , city , companylist} from "@/api/api";
import mapAll from "@/utils/mapAll.json";
import { option4 as option1 } from "../mapData.js";
var convertData = function (data) {
  var res = [];
  for (var i = 0; i < data.length; i++) {
    res.push({
      name: data[i].city_name.split('省')[0],
      value: [data[i].lng, data[i].lat, data[i].total],
    });
  }
  return res;
};
var myChart = null;

export default {
  data(){
    return{
      chainList:[],
      chain_id:'',
      isLoading:false,
      isloading:false,
      city_name:'',


      shiList:[],
      quList:[],
      shengList:[],
      sheng_id:'',
      shi_id:'',
      qu_id:'',
      city_id:1,
      cityItem:{city_name:''},
      chainItem:{},
      cityList:['中国'],
      chain_name:'',
      companyList:[],
      total:''
    }
  },
  created(){
    this.chain_name=this.$route.query.chain_name
    this.getCity()
    this.chainItem.chain_id=this.$route.query.chain_id||126791
    this.chainItem.cityItem=this.cityItem
    this.$nextTick(()=>{
      this.getLine(this.chainItem)
      this.getCompanyList(this.chainItem)
    })
  },
  mounted(){
    myChart = this.$echarts.init(document.getElementById('jjmap'))
    window.onresize = myChart.resize
    window.addEventListener("resize", myChart.resize)

    option1.series[0].map = "china";
    myChart.setOption(option1);
    myChart.on("click", (param)=>{
      this.changeMap(param.name)
    });
  },
  beforeDestroy(){
    if(myChart){
      myChart.clear()
    }
  },
  methods:{
    showList(){
      this.$emit('showList',{city_id:this.city_id,chain_id:this.chainItem.chain_id,chain_name:this.chain_name})
    },
    getCity(){
      city({city_id:1}).then(res=>{
        this.shengList=res.data
      })
    },
    shengChange(id,city_name=''){
      this.sheng_id=id
      this.qu_id = ''
      this.shi_id = ''
      this.quList=[]
      this.shiList=[]
      let index= this.shengList.findIndex(item=>item.id==id)
      if(index!=-1){
        this.shiList = this.shengList[index].children
        this.cityItem = this.shengList[index]
        this.cityList[1]=this.cityItem.city_name
        this.city_id=id
        this.chainAndCity()
        if(city_name){
          this.changeMap(city_name)
        }
      }else{
        this.cityItem = {city_name:''}
        this.city_id= 1
        this.chainAndCity()
      }
    },
    shiChange(id){
      this.shi_id=id
      this.qu_id = ''
      this.quList=[]
      let index= this.shiList.findIndex(item=>item.id==id)
      if(index!=-1){
        this.quList = this.shiList[index].children
        this.cityItem = this.shiList[index]
        this.cityList[2]=this.cityItem.city_name
        this.city_id=id
        this.chainAndCity()
      }else{
        this.cityItem = this.shengList.find(item=>item.id==this.sheng_id)
        this.city_id= this.sheng_id||1
        this.chainAndCity()
      }
    },
    chainAndCity(){
      this.chainItem.cityItem = this.cityItem
      this.getLine(this.chainItem)
      this.getCompanyList(this.chainItem)
    },


    getCompanyList(item){
      let data={
        chain_id:item.chain_id,
        city_id:this.cityItem.id||1,
        page:1,
        limit:10
      }
      companylist(data).then(res=>{
        this.companyList=res.data.data
        this.total = res.data.total
      })
    },
    toDetail(item){
      wx.miniProgram.navigateTo({
        url:'/pages/company/detail?identification='+item.identification
      })
    },

    changeCity(index,item){
      if(index==0){
        this.shengChange('')
      }else{
        this.changeMap(item,index)
      }
      this.cityList.splice(index+1,2-index)
    },
    changeMap(city_name,index){
      if(index==1){
        let item_city =this.shengList.find(item=>(item.city_name.indexOf(city_name)==0||city_name.indexOf(item.city_name)==0))
        if(item_city){
          this.shengChange(item_city.id,city_name)
        }
      }
      city_name=city_name.split("县")[0].split("区")[0]
      if(this.sheng_id){
        let item_city =this.shiList.find(item=>(item.city_name.indexOf(city_name)==0||city_name.indexOf(item.city_name)==0))
        if(item_city){
          this.shiChange(item_city.id)
        }
      }else{
        let item_city =this.shengList.find(item=>(item.city_name.indexOf(city_name)==0||city_name.indexOf(item.city_name)==0))
        if(item_city){
          this.shengChange(item_city.id,city_name)
        }
      }
    },



    getLine(item){
      if(!item.cityItem.city_name){
        option1.series[0].map = "china";
        myChart.setOption(option1);
      }else{
        // this.city_name = item.cityItem.city_name
        this.getMap(item.cityItem)
      }
      this.isLoading=true
      chain_city_map({city_id:item.cityItem.id,chain_id:this.$route.query.chain_id||'126791'}).then(res=>{
        this.lineList=res.data
        this.draw(this.lineList)
        this.isLoading=false
      })
    },
    getMap(cityItem){
      let city_code1 = mapAll.find(
        (item) =>
          cityItem.city_name.indexOf(item.name) == 0 ||
          item.name.indexOf(cityItem.city_name) == 0
      )
      if(!city_code1){
        this.isloading=false
        return
      }
      let city_code=city_code1.code
      getMap({ code: city_code, type: (cityItem.type != 3) ? "_full" : "" }).then(
        ({ data }) => {
          myChart.clear();
          this.$echarts.registerMap(city_code, data);
          option1.series[0].map = city_code;
          if (cityItem.type == 3) {
          } else {
          }
          myChart.setOption(option1, true);
          this.isloading = false;
        }
      ).catch(err=>{
        this.isloading=false
      })
    },
    draw(list){
      this.xData=[]
      this.yData=[]
      if(!list){
        return
      }
      option1.series[0].data = convertData(list)
      let list1 = list.sort(function(a, b) {
        return b.total- a.total;
       }).slice(0, 10)

      if(list1[0].total==0){
        option1.visualMap.max=1
      }else{
        option1.visualMap.max = list1[0].total
      }
      myChart.setOption(option1,true)
    }
  }
}
</script>
<style lang="scss" scoped>
.jjmap{
  min-height: 350px;
  // width: 100%;
  margin: 5px;
  border: 2px solid #ccc;
  border-radius: 5px;
  background: #f3f4fb;
  .header{
    margin: 5px;
    font-size: 18px;
    font-weight: 600;
  }
  position: relative;
}
#jjmap{
  width: 100%;
  height: 300px;
}
.city_list{
  display: flex;
  position: absolute;
  right: 10px;
  top: 5px;
  font-size: 16px;
  div{
    margin: 0 5px;
    font-weight: 600;
    color: rgb(8, 150, 252);
  }
}
.flex1{
  display: flex;
  height: 60px;
  align-items: center;
  border-bottom: 1px solid #dfdfdf;
  .c-left{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    min-width: 40px;
    margin-right: 10px;
    .score{
      color: #BE0010;
      font-size: 14px;
    }
  }
  .c-right{
    width: calc(100% - 60px);
    position: relative;
    .c-title{
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .c-b{
      font-weight: 600;
      color: #555;
      font-size: 12px;
    }
    &::after{
      content: ' ';
      position: absolute;
      right: -10px;
      height: 8px;
      width: 8px;
      top:18px;
      border-top: 1px solid rgb(9, 117, 179);
      border-right: 1px solid rgb(9, 117, 179);
      transform: rotate(45deg);
    }
  }
}
.cH{
  color: #515a6e;
}
.item-c{
  cursor: pointer;
  margin: 0 10px;
}
.com_list{
  border: 2px solid #ccc;
  border-radius: 5px;
  margin: 5px;
  .header_more{
    height: 36px;
    line-height: 36px;
    text-align: right;
    padding:0 20px;
    font-size: 15px;
    color: rgb(9, 117, 179);
    border-bottom: 1px solid #ccc;
    position: relative;
    &::after{
      content: ' ';
      position: absolute;
      right: 10px;
      height: 8px;
      width: 8px;
      top:13px;
      border-top: 1px solid rgb(9, 117, 179);
      border-right: 1px solid rgb(9, 117, 179);
      transform: rotate(45deg);
    }
    .title{
      position: absolute;
      left: 20px;
      color: #444;
      font-weight: 600;
    }
  }
  .list{
    height: 350px;
    overflow: auto;
  }
}
</style>